﻿using Mes.Pendant.PermissionService;
using Mes.Pendant.PermissionService.Dto;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.RBACIAppService.UserIAppService.Dto;
using Mes.Pendant.RBACIAppService.UserIAppService.IAppService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Users;

namespace Mes.Pendant.Web.Controllers.RBAC
{
	public class RBACController : Controller
	{
		private readonly IUserAppService _userIAppService;

		private readonly IPermissionServices permissionServices;

		public RBACController(IUserAppService userIAppService, IPermissionServices permissionServices)
		{
			_userIAppService = userIAppService;
			this.permissionServices = permissionServices;
		}

		public IActionResult UserList()
		{
			return View();
		}

		public IActionResult RoleList()
		{
			return View();
		}

		public IActionResult PermissionList()
		{
			return View();
		}

		public IActionResult AddUserView()
		{
			return View();
		}

		public IActionResult UpdateUserView(int id)
		{
			ViewBag.Id = id;
			return View();
		}

		public IActionResult AddPermissionView()
		{
			return View();
		}

		public IActionResult UpdPermissionView(int id)
		{
			ViewBag.Id = id;
			return View();
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="page"></param>
		/// <param name="limit"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<JsonResult> GetUserList(int page, int limit)
		{
			var userListDtoList = await _userIAppService.GetUserList(page, limit);
			return Json(new { data = userListDtoList.Queryable, count = userListDtoList.RowCount, code = 0, msg = "" });
		}

		[HttpPost]
		public async Task<int> AddUser(AddUserDto addUserDto)
		{
			await _userIAppService.AddUsers(addUserDto);
			return 1;
		}

		public async Task<int> DeleteUser(int id)
		{
			await _userIAppService.DeleteUser(id);
			return id;
		}

		[HttpGet]
		public async Task<UpdateUserDto> FindUser(int id)
		{
			UpdateUserDto updateUserDto = await _userIAppService.FindUser(id);
			return updateUserDto;
		}

		public async Task<int> UpdateUser(UpdateUserDto updateUserDto)
		{
			int id = await _userIAppService.UpdateUser(updateUserDto);
			return id;
		}

		/// <summary>
		/// 获取权限数据
		/// </summary>
		/// <param name="permissionName"></param>
		/// <param name="index"></param>
		/// <param name="limit"></param>
		/// <returns></returns>
		public async Task<JsonResult> GetPermissionList(string? permissionName, int index = 1, int limit = 10)
		{
			var permissionlist = await permissionServices.GetPermissionList(permissionName, index, limit);
			return Json(new { data = permissionlist.Queryable, count = permissionlist.RowCount, code = 0, msg = "success" });
		}

		/// <summary>
		/// 添加权限数据
		/// </summary>
		/// <param name="addPermissiondto"></param>
		/// <returns></returns>
		public async Task<int> AddPermission(AddPermissiondto addPermissiondto)
		{
			var perdto = await permissionServices.AddPermissions(addPermissiondto);
			return perdto;
		}

		/// <summary>
		/// 通过id获取权限数据
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<JsonResult> GetByidPermission(int id)
		{
			var perdto=await permissionServices.SelectByidPerm(id);
			return Json(new {code=0,msg="success",data=perdto});
		}

		/// <summary>
		/// 修改权限数据
		/// </summary>
		/// <param name="updPermissiondto"></param>
		/// <returns></returns>
		public async Task<int> UpdatePermissiondatas(AddPermissiondto updPermissiondto)
		{
			var perdto=await permissionServices.UpdatePermissions(updPermissiondto);
			return perdto;
		}

		/// <summary>
		/// 启用停用按钮
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<int> UpdStatesPer(int id)
		{
			var perdto = await permissionServices.EditStates(id);
			return perdto;
		}
 	}
}
